function init(){
    imgPreview();
    submitUpdateForm('.btn-submit-update', evtAfterUpdate);
    inlineSingleProductDeleteRow();
    showChangeImagePanel();
    uploadImage($('#frm-upload-img'), getUploadedImage);
    doCropImage(evtAfterCropImage);
    setProductImage();
    setAvatarProduct();

    showPanelUpdate('.item-list', getUpdateRow);
    changeRowState();
}

function getUpdateRow(row){
    var html = row.parents('table').find('tr.tr-temp').clone().removeClass('hidden').removeClass('tr-temp').addClass('tr-update-temp');
    html.find('#input-product-title').val(row.find('td.input-product a.product-title').text().trim());
    html.find('#input-product-slug').val(row.find('td.input-product a.product-slug').text().trim());
    html.find('#input-product-code').val(row.find('td.input-product .product-code').html());
    html.find('#input-product-cate').val(row.find('td.input-product-cate a.shop-cate').attr('data-id'));
    html.find('#input-product-price-old').val(row.find('td.input-product-price').attr('data-price-old'));
    html.find('#input-product-price-new').val(row.find('td.input-product-price').attr('data-price-new'));
    html.find('#input-product-active-flg').prop('checked', row.find('td.input-product-flg input.input-product-active-flg').is(':checked'));
    html.find('#input-product-sale-flg').prop('checked', row.find('td.input-product-flg input.input-product-sale-flg').is(':checked'));
    html.find('#input-product-delete-flg').prop('checked', row.find('td.input-product-flg input.input-product-delete-flg').is(':checked'));
    html.find('#input-product-id').val(row.attr('data-id'));
    html.find('#input-product-detail-id').val(row.attr('data-detail-id'));
    return html;
}

function evtAfterUpdate(data){
    var trEditting = $('tr.editting');
    var trTempUpdate = $('tr.tr-update-temp');
    if(trEditting.length==0 || trTempUpdate.length==0){
        window.history.replaceState({}, '', data.back_href);
        return;
    }
    trEditting.find('td.input-product a.product-title').html(trTempUpdate.find('#input-product-title').val());
    trEditting.find('td.input-product a.product-title').attr('href', data.back_href);
    trEditting.find('td.input-product a.product-slug').html(trTempUpdate.find('#input-product-slug').val());
    trEditting.find('td.input-product a.product-slug').attr('href', data.front_href);
    trEditting.find('td.input-product .product-code').html(trTempUpdate.find('#input-product-code').val());
    trEditting.find('td.input-product-cate a.shop-cate').attr('data-cate-id', trTempUpdate.find('#input-product-cate').val());
    trEditting.find('td.input-product-price').attr('data-price-old', trTempUpdate.find('#input-product-price-old').val());
    trEditting.find('td.input-product-price').attr('data-price-new' ,trTempUpdate.find('#input-product-price-new').val());
    trEditting.find('td.input-product-price span.price-old').html(formatCurrency(trTempUpdate.find('#input-product-price-old').val()) + "<sup>đ</sup>");
    trEditting.find('td.input-product-price span.price-new').html(formatCurrency(trTempUpdate.find('#input-product-price-new').val()) + '<sup>đ</sup>');
    trEditting.find('td.input-product-flg input.input-product-active-flg').prop('checked', trTempUpdate.find('#input-product-active-flg').is(':checked'));
    trEditting.find('td.input-product-flg input.input-product-sale-flg').prop('checked', trTempUpdate.find('#input-product-sale-flg').is(':checked'));
    trEditting.find('td.input-product-flg input.input-product-delete-flg').prop('checked', trTempUpdate.find('#input-product-delete-flg').is(':checked'));
}

function changeRowState(){
    $('.input-product-flg input[type="checkbox"]').change(function(){
        var url = $(this).parents('table').attr('data-href');
        var value = $(this).is(':checked')==true?1:0;
        var name = $(this).attr('data-name');
        var product = {id: $(this).parents('tr').attr('data-id')};
        product[name] = value;
        product['update_case']=0;
        changeState(url, {product: product});
    });
}

function addTempRowProduct(){
    var tempRow = $('#product-in-set-list thead tr.tr-temp').clone().removeClass('hidden');
    $('#product-in-set-list tbody').append(tempRow);
    $('#product-in-set-list tbody tr.tr-temp').find('input, textarea').each(function(i,j){
        var rowIndex = $(this).parents('tr').index()+1;
        var name = $(this).attr('data-name').replace("index", rowIndex);
        $(this).attr('name', name);
    });
    $('#product-in-set-list tbody tr.tr-temp').find('th.product-index').each(function(j,j){
        var rowIndex = $(this).parents('tr').index()+1;
        $(this).html(rowIndex);
    });
}

function inlineSingleProductDeleteRow(){
    $(document.body).on('click', '.inline-delete-row', function(){
        $(this).parents('tr').remove();
    });
}

function showChangeImagePanel(){
    $(document.body).on('click', 'div.img-control-product a', function(e){
        e.preventDefault();
        $(this).parents('tr').attr('need-change-image', '1');
        $('#modal-change-image').modal({
            backdrop: 'static'
        });
        return false;
    });
}

function setProductImage(){
    $(document.body).on('click', '.product-img-block', function(e){
        e.preventDefault();
        var folder = $(this).attr('data-folder');
        var file_name = $(this).attr('data-name');
        var file_path = $(this).find('img.media-object').attr('src');
        var row = $('#product-in-set-list tr[need-change-image=1]');
        row.find('div.has-change-img-control a.product-detail-img').attr('href', file_path).addClass('fancybox');
        row.find('div.has-change-img-control a.product-detail-img img').removeAttr('data-src').attr('src', file_path);
        row.find('div.has-change-img-control input.input-product-img').val(folder + file_name);
        $('#modal-change-image').modal('hide');
        $('#product-in-set-list tr').attr('need-change-image', '0');
        return false;
    });
}

function evtAfterCropImage(data){
    //add to temp
    var newImage = $('#product-temp-block').clone().removeAttr('id').removeClass('hidden');
    newImage.find('a.thumbnail').attr('data-name', data.file_name);
    newImage.find('img.media-object').attr('src', data.file_path);
    $('ul#image-temp-upload').append(newImage);
    //clear crop panel
    $('#input-crop-path').val('');
    $('.image-current').removeClass('hidden');
    $('.image-resize').addClass('hidden');
}

function setAvatarProduct(){
    $(document.body).on('change', '.input-product-detail-is-avatar', function(){
        if($(this).is(':checked')){
            //reset all other
            $('.input-product-detail-is-avatar').prop('checked', false);
            $(this).prop('checked', true);
        } else {
            var flg = 0;
            $('.input-product-detail-is-avatar').each(function(i,i){
                if($(this).is(':checked')){
                    flg=1;
                    return;
                }
            });
            if(flg==0){
                alert('Lỗi : Vui lòng chọn một sản phẩm chính !');
                $(this).prop('checked', true);
            }
        }
    });
}